<template>
    <div class="password-layout">
        <header-view>
            <img src="../../../assets/image/back.png" alt="" class="icon" @click="goBack">
            <div class="title">修改密码</div>
            <div class="icon"></div>
        </header-view>
        <div class="content">
            <ul class="change-layout">
                <li class="change-item">
                    <span class="label font-large24">原密码：</span><input type="password" v-model="oldPassword" ref="old">
                </li>
                <li class="change-item">
                    <span class="label font-large24">新密码：</span><input type="password" v-model="newPassword" ref="new">
                </li>
                <li class="change-item">
                    <span class="label font-large24">确认密码：</span><input type="password" v-model="newPassword2" ref="new2">
                </li>
            </ul>
            <div class="confirm" @click="onChange">确定</div>
        </div>
    </div>
</template>

<script>
    import HeaderView from '../../common/HeaderView';
    import md5 from 'js-md5'

    import { updatePassword } from '../../../api/user'

    export default {
        name: 'UpdatePassword',
        components: { HeaderView },
        data() {
            return {
                oldPassword: '',
                newPassword: '',
                newPassword2: ''
            }
        },
        mounted() {
            this.$refs.old.focus()
        },
        methods: {
            goBack() {
                this.$router.go(-1)
            },
            onChange() {
                if(this.oldPassword === '') {
                    this.$toast('请输入原密码')
                    this.$refs.old.focus()
                    return
                }
                if(this.newPassword === '') {
                    this.$toast('请输入新密码')
                    this.$refs.new.focus()
                    return
                }
                if(this.newPassword2 === '') {
                    this.$toast('请再输一次新密码')
                    this.$refs.new2.focus()
                    return
                }
                if(this.newPassword !== this.newPassword2) {
                    this.$toast('两次密码输入不一致')
                    this.$refs.new2.focus()
                    return
                }
                updatePassword(
                    md5(this.oldPassword),
                    md5(this.newPassword),
                    JSON.parse(localStorage.getItem('userInfo')).userId
                )
                    .then(response => {
                        console.log(response)
                        this.$toast('修改成功')
                        this.$router.push({name: 'login'})
                    })
                    .catch(error => {
                        console.log(error)
                    })
            }
        }
    }
</script>

<style scoped>
    .password-layout{
        background: #F5F8FB;
        min-height: 100vh;
        padding: 20px;
        margin-top: 135px;
    }
    .change-layout {
        width: 710px;
        /*height: 249px;*/
        background: #FFFFFF;
        box-shadow: 0 5px 10px 0 rgba(53, 191, 233, 0.09);
        border-radius: 30px;
        padding: 0 39px;
        box-sizing: border-box;
        margin-bottom: 10px;
    }
    .change-item {
        height: 90px;
        border-bottom: 1px solid rgba(0,0,0,0.1);
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    .change-item input {
        flex: 1;
    }
    .confirm {
        width: 549px;
        height: 129px;
        background-image: url("../../../assets/image/logout.png");
        background-size: 100%;
        background-repeat: no-repeat;
        color: #fff;
        text-align: center;
        font-size: 26px;
        line-height: 110px;
        margin: 66px auto 0;
    }

</style>